package btd.game.rule;
/*
 * A class to represent groups of stones.
 */

import java.util.HashSet;

@SuppressWarnings("serial")
public class Group extends HashSet<Stone>{
   private int freeSides;
   private int state;
   
   public HashSet<String> sidesPoints = new HashSet<String>();
   
   public Group() {
	   
   }
   
   public Group(Stone piece){
	   super();
       state = piece.getState();

       add(piece);
   }
   
   /**
    * Adds the specified Stone to this set if it is not already
    * present.
    *
    * @param piece Stone to be added to this set.
    * @return <tt>true</tt> if the set did not already contain the specified
    * Stone.
    */
   public boolean add(Stone piece){
      if(super.add(piece)){
    	  updateSides();
    	  return true;
      }
      return false;
   }
   
   public int getSides(){
      return freeSides;
   }
   
   public void updateSides(){
      freeSides = 0;
	  sidesPoints.clear();
      for(Stone m: this) {
    	  m.getFreeSides();
    	  for(String s : m.sidesPoints) {
    		  sidesPoints.add(s);
    	  }  
    	  //freeSides += m.getFreeSides();
    	  //freeSides = freeSides < m.getFreeSides() ? m.getFreeSides() : freeSides;
      }
      freeSides = sidesPoints.size();
   }
   
   public void updateSidesReverse(){
      freeSides = 0;
	  sidesPoints.clear();
      for(Stone m: this){
    	  m.getFreeSidesReverse();
    	  for(String s : m.sidesPoints) {
    		  sidesPoints.add(s);
    	  }
         //freeSides += m.getFreeSidesReverse();
         //freeSides = freeSides < m.getFreeSidesReverse() ? m.getFreeSidesReverse() : freeSides;
      }
	  freeSides = sidesPoints.size();
   }
   
   public int getBlackNeighbors(){
      int blacks = 0;
      for(Stone s : this){
         blacks += s.getBlackNeighbors();
      }
      
      return blacks;
   }
   
   public int getWhiteNeighbors(){
      int whites = 0;
      for(Stone s : this){
         whites += s.getWhiteNeighbors();
      }
      
      return whites;
   }
}
